package com.hyt.it.ogt.kq.service.gov.schedule.strategy;

import java.util.BitSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.hyt.it.ogt.kq.service.gov.schedule.impl.ExamRoomScheduleImpl.PackageExamRoomCandidate;
import com.hyt.it.ogt.kq.service.gov.schedule.model.Candidate;
import com.hyt.it.ogt.kq.service.gov.schedule.model.ExamRoom;
import com.hyt.it.ogt.kq.service.gov.schedule.model.ScheduleExamRoom.ScheduleCandidate;

/**
 * 编排辅助类
 * 缓存考生对应考场信息
 */

public class ScheduleHelper {
    private Map<String, ScheduleCandidate> candidateExamRoomMap = null;
    private Map<String, ExamRoom> examRoomMap = null;
    private static ThreadLocal<ScheduleHelper> instance = new ThreadLocal<ScheduleHelper>();;

    // 全局考生索引号，将各批次考生排重后再索引
    private Map<String, Integer> candidate2IndexMap;

    // 全局考生批次编码，如考生出现在1、2、4批次，则对应的编码是 1101
    // 用于生成考生在编排中虚拟考生ID号，该ID号可包含多名考生
    private Map<String, BitSet> candidateBatchFeatureMap;

    // // 考生ID对应编排虚拟考生ID号
    // private Map<String, String> candidateId2VirIdMap;

    // batchNo,<group name, candidate list>
    private Map<String, Map<String, List<Candidate>>> candidateGroupMap;

    private Map<String, List<ExamRoom>> examRoomGroupMap;

    // 批次号：<分组名：考生与考场的对应>
    private Map<String, Map<String, PackageExamRoomCandidate>> batchPackageGroupMap;

    // 虚拟考生ID对应相应批次考生ID号 Map<String(考生虚拟ID), Map<String(批次ID), String(考生ID)>>
    private Map<String, Map<String, String>> virId2CandidateIdMap;

    // 虚拟考生ID对应需考试的科目
    private Map<String, List<String>> virId2ExamSubjectsMap;

    // // 考场组包含的所有批次的考生, 考场组名：考生集合
    // private Map<String, Set<Candidate>> examRoomGroupCandidateMap;

    private Map<String, Set<String>> examRoomGroupIncludecandidateGroupMap;

    // 考生考试科目
    private Map<String, Set<String>> candidateSujectsMap;

    // 考生组名对应的考场组名
    private Map<String, String> candidateGroup2ExamRoomGroup;

    public ScheduleHelper() {
        this.candidateExamRoomMap = new HashMap<>();
        this.examRoomMap = new HashMap<>();
        this.candidate2IndexMap = new HashMap<>();
        this.candidateBatchFeatureMap = new HashMap<>();
        this.candidate2IndexMap = new HashMap<>();
        // this.candidateId2VirIdMap = new HashMap<>();
        this.virId2CandidateIdMap = new HashMap<>();
        this.candidateGroupMap = new HashMap<>();
        this.examRoomGroupMap = new HashMap<>();
        this.batchPackageGroupMap = new HashMap<>();
        this.candidateGroup2ExamRoomGroup = new HashMap<>();
        this.examRoomGroupIncludecandidateGroupMap = new HashMap<>();
        this.candidateSujectsMap = new HashMap<>();
        this.virId2ExamSubjectsMap = new HashMap<>();
    }

    public void clear() {
        this.examRoomMap.clear();
        this.candidateExamRoomMap.clear();
        this.candidate2IndexMap.clear();
        this.candidateBatchFeatureMap.clear();
        this.candidate2IndexMap.clear();
        // this.candidateId2VirIdMap.clear();
        this.virId2CandidateIdMap.clear();
        this.candidateGroupMap.clear();
        this.examRoomGroupMap.clear();
        this.batchPackageGroupMap.clear();
        this.examRoomGroupIncludecandidateGroupMap.clear();
        this.candidateGroup2ExamRoomGroup.clear();
        this.candidateSujectsMap.clear();
        this.virId2ExamSubjectsMap.clear();
    }

    public static ScheduleHelper getInstance() {
        if (instance.get() == null) {
            ScheduleHelper scheduleHelper = new ScheduleHelper();
            instance.set(scheduleHelper);
            return scheduleHelper;
        } else {
            return instance.get();
        }
    }
    
    public void unload() {
        instance.remove(); // Compliant
    }

    public Map<String, ScheduleCandidate> getCandidateExamRoomMap() {
        return this.candidateExamRoomMap;
    }

    public Map<String, ExamRoom> getExamRoomMap() {
        return this.examRoomMap;
    }

    public Map<String, Integer> getCandidate2IndexMap() {
        return this.candidate2IndexMap;
    }

    public Map<String, BitSet> getCandidateBatchFeatureMap() {
        return this.candidateBatchFeatureMap;
    }

    // public Map<String, String> getCandidateId2VirIdMap() {
    // return this.candidateId2VirIdMap;
    // }

    public Map<String, Map<String, String>> getVirId2CandidateIdMap() {
        return this.virId2CandidateIdMap;
    }

    public Map<String, Map<String, List<Candidate>>> getCandidateGroupMap() {
        return this.candidateGroupMap;
    }

    public Map<String, List<ExamRoom>> getExamRoomGroupMap() {
        return this.examRoomGroupMap;
    }

    public void setExamRoomGroupMap(Map<String, List<ExamRoom>> examRoomGroupMap) {
        this.examRoomGroupMap = examRoomGroupMap;
    }

    public Map<String, Map<String, PackageExamRoomCandidate>> getBatchPackageGroupMap() {
        return this.batchPackageGroupMap;
    }

    public Map<String, Set<String>> getExamRoomGroupIncludecandidateGroupMap() {
        return this.examRoomGroupIncludecandidateGroupMap;
    }

    public Map<String, String> getCandidateGroup2ExamRoomGroup() {
        return this.candidateGroup2ExamRoomGroup;
    }

    public Map<String, Set<String>> getCandidateSujectsMap() {
        return this.candidateSujectsMap;
    }

    public Map<String, List<String>> getVirId2ExamSubjectsMap() {
        return this.virId2ExamSubjectsMap;
    }

}
